package com.opennews.openplatform

import groovy.transform.CompileStatic
import org.springframework.http.HttpStatus

@CompileStatic
class AccountGroupUserController extends BaseController {
    static allowedMethods = [
        remove: "POST",
        quit: "POST"
    ]

    AccountGroupUserService accountGroupUserService

    // region: Get actions.
    def index() {
        render("AccountGroupUserController")
    }

    def queryAvailableAccountGroups() {
        def userId = getClaims().id
        def result = accountGroupUserService.queryAvailableAccountGroups(userId)

        sendDataResponse(result)
    }
    // endregion

    // region: Post actions.
    def remove() {
        def requestBodyData = getRequestBodyData()
        def userId = requestBodyData.userId.toString()
        def username = requestBodyData.username.toString()
        def claim = getClaims()
        def accountGroupId = claim.accountGroupId
        accountGroupUserService.remove(accountGroupId, userId, username)

        sendStatusResponse(HttpStatus.NO_CONTENT)
    }

    def quit() {
        def requestBodyData = getRequestBodyData()
        def accountGroupId = requestBodyData.accountGroupId.toString()
        def claim = getClaims()
        def userId = claim.id
        def username = claim.username
        accountGroupUserService.remove(accountGroupId, userId, username)

        sendStatusResponse(HttpStatus.NO_CONTENT)
    }
    // endregion
}
